<?php
/**
 * Created by PhpStorm.
 * User: xsp
 * Date: 2018/4/18
 * Time: 8:53
 */

namespace backend\controllers;

use backend\models\Admin;
use Yii;
use yii\data\Pagination;
use yii\web\Controller;

class ManageController extends CommonController
{

    protected $mustLogin = ['managers','addmanager','emailchangerpass','del'];
    public function actionManagers()
    {
        $this->layout = 'main';
        $model = Admin::find();
        $count = $model->count();
        $page = new  Pagination(['totalCount'=>$count,'pageSize'=>'3']);
        $manager = $model->offset($page->offset)->limit($page->limit)->all();
        return $this->render('managers',['manager'=>$manager,'page'=>$page]);
    }

    public function actionAddmanager()
    {
        $model = new Admin();
        if(Yii::$app->request->isPost)
        {
            if($model->Addmanager(Yii::$app->request->post()))
            {
                Yii::$app->session->setFlash('info','添加成功');

            }else{
                Yii::$app->session->setFlash('info','添加失败');
            }
        }
        return $this->render('addmanager',['model'=>$model]);
    }
    public function actionEmailchangepass()
   {
       $this->layout='login';

           $time =  Yii::$app->request->get('timestamp');
           $adminuser = Yii::$app->request->get('adminuser');
           $token =  Yii::$app->request->get('token');
           $model = new Admin();
           $mytoken =  $model->createToken($adminuser,$time);
           if($token!=$mytoken)
           {
               $this->redirect(['public/login']);
               Yii::$app->end();
           }
           if(time()-$time>300)
           {
               $this->redirect(['public/login']);
               Yii::$app->end();
           }
       if(Yii::$app->request->isPost)
       {
           $post = Yii::$app->request->post();
          if ($model->changepass($post))
          {
              Yii::$app->session->setFlash('info','密码修改成功');
          }
       }
       $model->adminuser = $adminuser;
       return $this->render('emailchangepass',['model'=>$model]);
   }
   public function actionDel()
   {
       $adminid =(int)Yii::$app->request->get('adminid');
       if(empty($adminid))
       {
            $this->redirect(['manage/managers']);
       }
       $model = new Admin();

       if($model->deleteAll(['adminid'=>$adminid]))
       {
           Yii::$app->session->setFlash('info','删除成功');
            $this->redirect(['manage/managers']);
       }
   }
}